const router = require('koa-router')()
const { saveRoleForPermission, findPermissions } = require('../../../controller/r_role_permission');
const { addRole, getRoleList, deleteDocuments } = require("../../../controller/system/user/ROLE.CONTROLLER");
const { serverError } = require('../../../utils');
const { emptyCheck } = require('../../../utils/checkEmpty');
const pageSizeList = require('../../../utils/slicePageSize');
const addUserForRole = require("../../../controller/system/user/r_role_user").saveDocument;

router.prefix("/role");

// add role
router.post("/add", async (ctx, next) => {
    try {
        const { roleName, } = ctx.request.body;
        const check = emptyCheck({ roleName })
        if (check.code !== 0) {
            ctx.body = check;
        } else {
            const res = await addRole({ roleName })
            ctx.body = res
        }
    } catch (error) {
        console.log("服务器故障：", error)
        ctx.body = serverError
    }
})


//getRoleList
router.get("/list", async (ctx, next) => {
    try {
        const { roleId, roleName, pageSize, current } = ctx.request.body;
        const data = await getRoleList({ roleId, roleName });
        ctx.body = {
            code:0,
            msg: "查询成功",
            data: {
                total: data.length,
                list: pageSizeList({ data, pageSize, current }).map(item => ({
                    roleName: item.roleName,
                    roleId: item._id,
                }))
            }
        }
    } catch (error) {
        ctx.body = serverError
    }

})


// delete
router.post("/delete", async (ctx, next) => {
    try {
        const { ids = [] } = ctx.request.body
        if (ids.length === 0) {
            ctx.body = {
                status: '0011',
                message: "未选择需要删除的数据"
            }
        }
        else {
            const res = await deleteDocuments({ ids: ids })
            ctx.body = res;
        }
    } catch (error) {
        ctx.body = serverError
    }
})

// add user for role;
router.post("/addUserForRole", async (ctx, next) => {
    try {
        const { userId, roleId, roleName, userName, user_id } = ctx.request.body;
        const checkEmpty = emptyCheck({ userId, roleId, roleName, userName, user_id })
        if (checkEmpty.code !== 0) {
            ctx.body = checkEmpty;
        } else {
            const res = await addUserForRole({
                userId, roleId, roleName, userName, user_id
            })
            ctx.body = res
        }

    } catch (error) {
        console.log("报错原因：",error)
        ctx.body = serverError;
    }
})

/**@description 保存角色拥有的权限 */
router.post("/savePermissionForRole", async (ctx, next) => {
    try {
        const { role_id, roleName, menu_ids = [] } = ctx.request.body;
        const res = await saveRoleForPermission({
            role_id: role_id, roleName, menu_ids: menu_ids
        })
        ctx.body = res;
    } catch (error) {
        ctx.body = serverError;
    }
})


// getPermission for role
router.post("/getPermissionForRole", async (ctx, next) => {
    try {
        const { role_id } = ctx.request.body;
        const res = await findPermissions({
            role_id
        })
        ctx.body = {
            status: "0000",
            message: "查询成功",
            data: {
                list: [...res]
            }
        }

    } catch (error) {
        ctx.body = serverError;
    }
})




module.exports = router